package cn.miju.dict;

import cn.hutool.core.collection.CollUtil;
import cn.miju.database.entity.SysDict;
import cn.miju.database.entity.SysDictDetail;
import cn.miju.database.mapper.SysDictDetailMapper;
import cn.miju.database.mapper.SysDictMapper;
import cn.miju.dict.constant.DictConstant;
import cn.miju.dict.model.DictItem;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Maps;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <pre>
 * 数据字典系统启动缓存
 * </pre>
 *
 * @author zhuming
 * @version 1.0.0
 * @since 2023-04-23 9:25
 */
@Component
@RequiredArgsConstructor
public class DictStartCacheConfig {

    private final SysDictMapper sysDictMapper;
    private final SysDictDetailMapper sysDictDetailMapper;
    private final StringRedisTemplate redisTemplate;

    @PostConstruct
    public void invoke() {
        List<SysDict> sysDictList = sysDictMapper.selectList(Wrappers.lambdaQuery());
        if (CollUtil.isNotEmpty(sysDictList)) {
            List<SysDictDetail> sysDictDetailList = sysDictDetailMapper.selectList(Wrappers.lambdaQuery());
            Map<Long, List<SysDictDetail>> sysDictDetailGroupMap = Maps.newHashMap();
            if (CollUtil.isNotEmpty(sysDictDetailList)) {
                sysDictDetailGroupMap.putAll(sysDictDetailList.stream().collect(Collectors.groupingBy(SysDictDetail::getDictId,
                        Collectors.mapping(sysDictDetail -> sysDictDetail, Collectors.toList()))));
            }
            SetOperations<String, String> setOperations = redisTemplate.opsForSet();
            ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
            sysDictList.forEach(sysDict -> {
                List<SysDictDetail> sysDictDetailByDictIdList = sysDictDetailGroupMap.getOrDefault(sysDict.getId(), null);
                if (CollUtil.isNotEmpty(sysDictDetailByDictIdList)) {
                    String key = DictConstant.Redis.DICT_VALIDATE_KEY + sysDict.getDictCode();
                    if (Boolean.TRUE.equals(redisTemplate.hasKey(key))) {
                        redisTemplate.delete(key);
                    }
                    setOperations.add(key, sysDictDetailByDictIdList.stream().map(SysDictDetail::getSubValue).toArray(String[]::new));
                    key = DictConstant.Redis.DICT_LIST_KEY + sysDict.getDictCode();
                    if (Boolean.TRUE.equals(redisTemplate.hasKey(key))) {
                        redisTemplate.delete(key);
                    }
                    valueOperations.set(key, JSONArray.toJSONString(sysDictDetailByDictIdList.stream().
                            map(sysDictDetail -> DictItem.builder().value(sysDictDetail.getSubValue()).label(sysDictDetail.getSubName()).build()).
                            collect(Collectors.toList())));
                }
            });
        }
    }
}
